package com.hyx.api.controller.pc_admin;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hyx.api.service.pc_admin.IAdminService;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.hyx.api.domain.bo.ModelBo;
import com.hyx.api.domain.vo.ModelVo;
import com.hyx.api.service.IModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机型Controller    后端接口
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/model")
public class ModelController extends BaseController
{
    private final IModelService modelService;

    private final IAdminService adminService;

    /**
     * 查询机型列表
     */
    @PreAuthorize("@ss.hasPermi('api:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelBo bo)
    {
        startPage();
        List<ModelVo> list = modelService.selectModelList(bo);
        return getDataTable(list);
    }

    /**
     * 导出机型列表
     */
    @PreAuthorize("@ss.hasPermi('api:model:export')")
    @Log(title = "机型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelBo bo)
    {
        List<ModelVo> list = modelService.selectModelList(bo);
        ExcelUtil<ModelVo> util = new ExcelUtil<>(ModelVo.class);
        util.exportExcel(response, list, "机型数据");
    }

    /**
     * 获取机型详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(modelService.selectModelById(id));
    }

    /**
     * 新增机型
     */
    @PreAuthorize("@ss.hasPermi('api:model:add')")
    @Log(title = "机型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ModelBo bo)
    {
        return toAjax(modelService.insertModel(bo));
    }

    /**
     * 修改机型
     */
    @PreAuthorize("@ss.hasPermi('api:model:edit')")
    @Log(title = "机型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ModelBo bo)
    {
        return toAjax(modelService.updateModel(bo));
    }

    /**
     * 删除机型
     */
    @PreAuthorize("@ss.hasPermi('api:model:remove')")
    @Log(title = "机型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(modelService.deleteModelByIds(ids));
    }

    /**
     * 机型上传接口
     */
    @PostMapping("/importModels")
    public AjaxResult importModels(@RequestParam MultipartFile file){
        return adminService.importModels(file);
    }

}
